package com.booter.producer;

import com.booter.common.NetworkUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.util.Properties;

@EnableDiscoveryClient
@SpringBootApplication(exclude = {})
//@EnableTransactionManagement(proxyTargetClass = true)   //开启事物管理功能
public class SpringBootProducerApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        //设置客户机的的ip和端口
//        InetAddress address = InetAddress.getLocalHost();
//        System.setProperty("spring.cloud.consul.discovery.hostname", address.getHostAddress());
//        System.setProperty("spring.cloud.consul.discovery.port", "8081");
        SpringApplication.run(SpringBootProducerApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        try {
            Properties properties = PropertiesLoaderUtils.loadProperties(new PathMatchingResourcePatternResolver().getResource("classpath:sentinel.properties"));
            properties.forEach((k, v) -> {
                System.setProperty((String) k, (String) v);
            });
            //若本机是多网卡，需要绑定特定的ip
            System.setProperty("csp.sentinel.heartbeat.client.ip", NetworkUtils.getLocalIP());
        } catch (IOException ex) {
            this.logger.error("加载sentinel配置异常", ex);
        }
        return application.sources(SpringBootProducerApplication.class);
    }
}
